package com.banzhi.shop.fragment;

import android.support.v4.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.activity.CommodityManagementActivity;
import com.banzhi.shop.activity.CustomerManagingActivity;

import com.banzhi.shop.activity.InviteShopkeeperShareActivity;
import com.banzhi.shop.activity.MarketManagingActivity;
import com.banzhi.shop.activity.NewLoginActivity;
import com.banzhi.shop.activity.OrderManagementActivity;
import com.banzhi.shop.activity.OrderManagementActivity2;
import com.banzhi.shop.activity.SaleRankingActivity;
import com.banzhi.shop.activity.ShopInformationActivity;
import com.banzhi.shop.entity.Store.Store_Data;
import com.banzhi.shop.entity.WXshare;
import com.banzhi.shop.model.CallBack.StoreFrgCallBack;
import com.banzhi.shop.model.CallBack.WXshareCallBack;
import com.banzhi.shop.model.IStoreFrgModel;
import com.banzhi.shop.model.Impl.StoreFrgImpl;
import com.banzhi.shop.model.Impl.WXshareImpl;
import com.banzhi.shop.model.WXshareModel;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.view.CircleImageView;
import com.banzhi.shop.view.TitleBar;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import static com.banzhi.shop.base.BaseActivity.finishAllActivity;

/**
 * 店铺
 */

public class StoreFrg extends Fragment implements View.OnClickListener, StoreFrgCallBack, WXshareCallBack, SwipeRefreshLayout.OnRefreshListener {

    public static final String TAG = "StoreFrg";
    private View view;
    /**
     * 邀请店主
     */
    private LinearLayout llyaoqin;
    /**
     * 店铺管理
     */
    private LinearLayout llmanagement;
    /**
     * 推荐店铺
     */
    private LinearLayout lltuijian;
    /**
     * 订单管理
     */
    private LinearLayout llorder;
    /**
     * 销售管理
     */
    private LinearLayout llxiaoshou;
    /**
     * 客户管理
     */
    private LinearLayout llkehu;
    /**
     * 销售排行
     */
    private LinearLayout llpaihang;
    /**
     * 今日订单
     */
    private LinearLayout ll_today;
    /**
     * 店铺编号
     */
    private TextView tvbianhao;
    /**
     * 今日订单
     */
    private TextView tvdingdan;
    /**
     * 本月销售
     */
    private TextView tvxiaoshou;
    /**
     * 累计访问
     */
    private TextView tvfangwen;
    /**
     * 推荐店铺
     */
    private TextView tvdianpu;
    /**
     * 店铺logo
     */
    private CircleImageView ivlogo;
    /**
     * 店铺名字
     */
    private TextView tvname;
    private LinearLayout llright;
    private IStoreFrgModel model;
    private Store_Data data;
    private TitleBar titleBar;
    /**
     * 用于存储该商品的
     */
    private String wxURL;

    private static final int PENGYOUQUAN = 1;
    private static final int WEIXINHAOYOU = 2;
    private static final int LIANJIE = 3;
    private static final int ERWEIMA = 4;
    private WXshareModel wxmodel;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frg_store, container, false);
        initView();
        setListeners();
        model = new StoreFrgImpl();
        wxmodel = new WXshareImpl();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        model.getData(this);
    }

    private void initView() {
        titleBar = (TitleBar) view.findViewById(R.id.tb_store);
        titleBar.setTitle("半职商城");
        titleBar.setRightSkip(R.mipmap.share1, "", this);
        llyaoqin = (LinearLayout) view.findViewById(R.id.ll_yaoqin);
        llmanagement = (LinearLayout) view.findViewById(R.id.ll_management);
        lltuijian = (LinearLayout) view.findViewById(R.id.ll_tuijian);
        llorder = (LinearLayout) view.findViewById(R.id.ll_Order);
        llxiaoshou = (LinearLayout) view.findViewById(R.id.ll_xiaoshou);
        llpaihang = (LinearLayout) view.findViewById(R.id.ll_paihang);
        ll_today = (LinearLayout) view.findViewById(R.id.ll_today);
        tvbianhao = (TextView) view.findViewById(R.id.tv_bianhao);
        tvdingdan = (TextView) view.findViewById(R.id.tv_dingdan);
        tvxiaoshou = (TextView) view.findViewById(R.id.tv_xiaoshou);
        tvfangwen = (TextView) view.findViewById(R.id.tv_fangwen);
        tvdianpu = (TextView) view.findViewById(R.id.tv_dianpu);
        ivlogo = (CircleImageView) view.findViewById(R.id.iv_logo);
        tvname = (TextView) view.findViewById(R.id.tv_name);
        llright = (LinearLayout) view.findViewById(R.id.ll_right);
        llkehu = (LinearLayout) view.findViewById(R.id.ll_kehu);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.srl_listview);
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        swipeRefreshLayout.setColorSchemeResources(R.color.themeColor);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    private void setListeners() {
        llyaoqin.setOnClickListener(this);
        llmanagement.setOnClickListener(this);
        llorder.setOnClickListener(this);
        lltuijian.setOnClickListener(this);
        llxiaoshou.setOnClickListener(this);
        llright.setOnClickListener(this);
        llkehu.setOnClickListener(this);
        llpaihang.setOnClickListener(this);
        ll_today.setOnClickListener(this);
        titleBar.setRightSkip(R.mipmap.share1, null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSharePopupwindow();
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_yaoqin:
                // Intent yqintent = new Intent(getActivity(), InviteShopkeeperActivity.class);
                Intent yqintent = new Intent(getActivity(), InviteShopkeeperShareActivity.class);
                startActivity(yqintent);
                break;
                //店铺管理
            case R.id.ll_management:
                Intent CMintent = new Intent(getActivity(), CommodityManagementActivity.class);
                startActivity(CMintent);
                break;
                //订单管理
            case R.id.ll_Order:
              //  Intent OMintent = new Intent(getActivity(), OrderManagementActivity.class);
                Intent OMintent = new Intent(getActivity(), OrderManagementActivity2.class);
                startActivity(OMintent);
                break;
            case R.id.ll_right:
                Intent SIintent = new Intent(getActivity(), ShopInformationActivity.class);
                startActivity(SIintent);
                break;
            case R.id.ll_tuijian:
                Intent intent = new Intent(getActivity(), CustomerManagingActivity.class);
                intent.putExtra("num", 2);
                startActivity(intent);
                break;
            case R.id.ll_xiaoshou:
                Intent mmaintent = new Intent(getActivity(), MarketManagingActivity.class);
                startActivity(mmaintent);
                break;
            case R.id.ll_kehu:
                Intent cmaintent = new Intent(getActivity(), CustomerManagingActivity.class);
                cmaintent.putExtra("num", 1);
                startActivity(cmaintent);
                break;
            case R.id.ll_paihang:
                Intent xsintent = new Intent(getActivity(), SaleRankingActivity.class);
                startActivity(xsintent);
                break;

            //订单管理
            case R.id.ll_today:
                //Intent OM1intent = new Intent(getActivity(), OrderManagementActivity.class);
                 Intent OM1intent = new Intent(getActivity(), OrderManagementActivity2.class);
                 OM1intent.putExtra("type", 1);
                 startActivity(OM1intent);
                break;
        }
    }

    /**
     * 展示界面
     */
    private void showViews() {
        tvname.setText(data.getShopName());
        if (!"".equals(data.getShopLogo()) && data.getShopLogo() != null) {
            Picasso.with(getContext()).load(data.getShopLogo()).placeholder( R.mipmap.zhanweitu2).into(ivlogo);
        }
        tvdingdan.setText(data.getTodayOrder());
        tvxiaoshou.setText(data.getMonthSaleSum());
        tvfangwen.setText(data.getTotalView());
        tvdianpu.setText(data.getRecommendNum());
        tvbianhao.setText(data.getNumber());

        SharedPreferences.Editor editor = MyApp.loginMessage.edit();
        editor.putString("ShopName", data.getShopName());
        editor.putString("ShopLogo", data.getShopLogo());
        editor.apply();
    }

    /**
     * @param object 获取数据成功
     */
    @Override
    public void ongetStoreFrgDataSuccess(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        LoadingDialogUtils.dismiss();
        data = (Store_Data) object;
        showViews();
    }


    /**
     * @param object 获取数据失败
     */
    @Override
    public void ongetStoreFrgDataError(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }


    /**
     * @param object
     * @param num    微信分享成功
     */
    @Override
    public void ongetWXdataSuccess(Object object, int num) {
        LoadingDialogUtils.dismiss();
        WXshare share = (WXshare) object;
        wxURL = share.getRes();
        if (num == LIANJIE) {
            ShareUtils.copyLianjie(getActivity(), wxURL);
        } else if (num == WEIXINHAOYOU) {
            ShareUtils.wxshare(MyApp.loginMessage.getString("ShopName", ""), MyApp.loginMessage.getString("personAvatar", ""), "全球十佳网商联合出品。匠心工厂，源头供货，正品优品，尽在半职。", wxURL);
        } else if (num == PENGYOUQUAN) {
            ShareUtils.wxPengyouquan(data.getShopName(), data.getShopLogo(), wxURL);
        } else if (num == ERWEIMA) {
            ShareUtils.ereimakaidian6(getActivity(), MyApp.loginMessage.getString("ShopName", ""), "", null, null, null, data.getShopLogo(), wxURL);
        }
    }

    /**
     * @param object
     * @param num    微信分享失败
     */
    @Override
    public void ongetWXdataError(Object object, int num) {
        JSONObject jsonObject = (JSONObject) object;
        if ("1".equals(jsonObject.optString("code"))) {
            Intent intent = new Intent(getActivity(), NewLoginActivity.class);
            startActivity(intent);
            finishAllActivity();
        }
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    /**
     * 分享店铺
     */
    private void showSharePopupwindow() {
        //设置contentView
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.popupwindow_share_dianzhu, null);
        final PopupWindow popWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popWindow.setContentView(contentView);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失

        ImageView ivchacha = (ImageView) contentView.findViewById(R.id.iv_chacha);
        LinearLayout llpengyouquan = (LinearLayout) contentView.findViewById(R.id.ll_pengyouquan);
        LinearLayout llweixin = (LinearLayout) contentView.findViewById(R.id.ll_weixing);
        LinearLayout lllianjie = (LinearLayout) contentView.findViewById(R.id.ll_lianjie);
        LinearLayout llerweima = (LinearLayout) contentView.findViewById(R.id.ll_erweima);
        RelativeLayout rlbackground = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rlbackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        lllianjie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wxURL == null) {
                    LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
                    wxmodel.getData(null, "2", LIANJIE, StoreFrg.this);
                } else {
                    ShareUtils.copyLianjie(getActivity(), wxURL);
                }
            }
        });
        llweixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wxURL == null) {
                    LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
                    wxmodel.getData(null, "2", WEIXINHAOYOU, StoreFrg.this);
                } else {
                    ShareUtils.wxshare(MyApp.loginMessage.getString("ShopName", "") ,  data.getShopLogo(), "全球十佳网商联合出品。匠心工厂，源头供货，正品优品，尽在半职。", wxURL);
                }
            }
        });
        llpengyouquan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wxURL == null) {
                    LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
                    wxmodel.getData(null, "2", PENGYOUQUAN, StoreFrg.this);
                } else {
                    ShareUtils.wxPengyouquan(data.getShopName(), data.getShopLogo(), wxURL);
                }
            }
        });
        llerweima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wxURL == null) {
                    LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
                    wxmodel.getData(null, "2", ERWEIMA, StoreFrg.this);
                } else {
                    ShareUtils.ereimakaidian6(getActivity(), MyApp.loginMessage.getString("ShopName", ""), "", null, null, null, data.getShopLogo(), wxURL);
                }
            }
        });
        ivchacha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wxURL = null;
                popWindow.dismiss();
            }
        });
        ColorDrawable dw = new ColorDrawable(0x70000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.showAtLocation(contentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void onRefresh() {
        model.getData(this);
    }
}
